<?php
$menu = "transfer_bonus.php";
header("Cache-Control: no-store, no-cache, must-revalidate");
require("cek_opr.inc.php");
require("function.inc.php");
require("header.inc.php");

function give_bonus ($agenid, $jml,$tglawal,$tglakhir) {
	global $_GPesan;

	$curr = getcurr_balance($agenid);
	$sql_mutasi = "insert into mutasi (tanggal, agenid, ket, debet, lastbalance, currbalance) values (NOW(), '$agenid', 'BONUS Tgl $tglawal s.d. $tglakhir :$jml [$opr]', '$jml', $curr, $curr+$jml)";
	$sql_keu = "insert into trkeuangan (tanggalupd, kode, ket, kredit, opr) values (NOW(),'017','Bonus $agenid ',$jml, '$opr')";
	$sql_balance = "update stockiest set balance = balance + $jml where agenid='$agenid'";


	if (mysql_query($sql_mutasi) && mysql_query($sql_balance)) {
		echo "<li> Bonus $agenid jml : ".number_format($jml, 2, ".",",")."</li>\n";
	} else {
		echo "<li> Error...    $sql_mutasi $sql_balance  </li";
	}

	mysql_query($sql_keu);

	$rjml = number_format($jml, 2, ".",",");
	$sms1 = isi_sms("[company],[agenid],[nama],[jml],[awal],[akhir]","$_GNamaCompany,$agenid,$nama,$jml,$tglawal,$tglakhir",$_GPesan['PesanBonus']);
	$sms = str_replace("[jumlah]", number_format($jml, 2, ",","."),$sms1);

	send_sms($agenid, $sms, 0, $_BBonus, 1);
}

global $opr,$tglakhir;
$opr = $_SESSION['USERNAME'];

?>
<script language="javascript">
      $(document).ready(function() {
		$('#pandu').hide();
		$("#t").click(function () {
		$("#pandu").toggle();
		});
      });
</script>
<!-- <form name='form1' method="GET" id='form1'> -->
<div class='pandu' id="pandu">
	<p>Fungsi transfer ini untuk menarik bonus tiap transaksi user ke
		distributor</p>
</div>
<table width="100%" border="0" cellspacing="1" cellpadding="12"
	bgcolor="#FFFFFF">

	<tr>
		<td bgcolor="#F3EFF7" class='bg'>
			<table width="100%" border="0" cellspacing="2" cellpadding="2"
				bgcolor="#DEDAEC">
				<tr>
					<td bgcolor colspan=2>
						<div class='pandu-atas'>
							<img src="images/help.gif" align="middle" id="t" alt="Show Help"
								border="0"
								style="margin-right: 3px; margin-top: 0px; margin-bottom: 3px; cursor: pointer;" />
							<font size="+1">Tranfer Bonus untuk Upline</font>
						</div>
					</td>
				</tr>
			</table>
			<ul>
			<?
			if (!$_POST['tglmin']) {
				$rs = mysql_query("select date_format(tanggal, '%d-%m-%Y') as tgl  from mutasi_bonus where transfer = 0 limit 1");
				if ($r = mysql_fetch_object($rs)) {
					$tglmin = $r->tgl;
				}
				mysql_free_result($rs);
			} else {
				$tglmin = $_POST['tglmin'];
			}

			$tgl = ($_POST['tanggal']) ? $_POST['tanggal'] : date("d-m-Y");
			$tglakhir = substr($tgl,6).substr($tgl,3,2).substr($tgl,0,2);
			$tglawal = substr($tglmin,6).substr($tglmin,3,2).substr($tglmin,0,2);
			$jmlbaru = 0;

			if ($_POST['backup'] == "Transfer") {
				$jmldata = $_POST['jml'];
				for ($i=0;$i<$jmldata;$i++) {
					if ($_POST['cbx'][$i]) {

						$rs = s_query("m.agenid, sum(m.jml) as total","mutasi_bonus m ","1 AND (m.agenid = '".$_POST['cbx'][$i]."') AND (TO_DAYS(m.tanggal) >= TO_DAYS('$tglawal')) AND (TO_DAYS(m.tanggal) <= TO_DAYS('$tglakhir')) and (m.transfer = 0) and (m.hitung=1) group by m.agenid",0);
						while ($r = mysql_fetch_object($rs)) {
							echo "<li> Mentransfer bonus upline ...($r->total) $r->agenid </li>\n";
							give_bonus ($r->agenid, $r->total, $tglmin, $tgl);
						}

						$sql = "update mutasi_bonus m
					set m.transfer = 1
					where 1 AND (m.agenid = '".$_POST['cbx'][$i]."') AND (TO_DAYS(m.tanggal) >= TO_DAYS('$tglawal')) AND (TO_DAYS(m.tanggal) <= TO_DAYS('$tglakhir')) and (m.transfer = 0) and (m.hitung=1)";
						mysql_query($sql);
					}
				}
			} else if ($_POST['backup'] == "Refresh") {

				$rs = s_query("m.agenid, sum(m.jml) as total, count(m.agenid) as jumlah","mutasi_bonus m "," (TO_DAYS(m.tanggal) >= TO_DAYS('$tglawal')) AND (TO_DAYS(m.tanggal) <= TO_DAYS('$tglakhir')) and (m.transfer = 0) and (m.hitung=1) group by m.agenid",0);
				$jmlrec = 0;
				$strtampil = "<table width='400' cellspacing=1 cellpadding=4 bgcolor='#CFCFCF'>";
				while ($r = mysql_fetch_object($rs)) {
					$strtampil .= "<tr bgcolor='white'><td> $r->agenid </td><td width=40 align=center> $r->jumlah </td><td align=right>".number_format($r->total, 2, ",", ".")."</td><td width=40><input type=checkbox name=cbx[$jmlrec] value='$r->agenid'></td></tr> \n";
					$jmlrec++;
					$jmltotal += $r->total;
					$jmljumlah += $r->jumlah;
				}
				$strtampil .= "<tr bgcolor='#AfAfAf'><td> Total </td><td width=40 align=center> $jmljumlah </td><td align=right>".number_format($jmltotal, 2, ",", ".")."</td><td width=40>&nbsp;</td></tr> \n";
				$strtampil .= "</table> \n";

			}
		 ?>
			</ul>
			<form method="POST">
				<input type=hidden name='jml' value='<?=$jmlrec?>'> Tanggal Min :<input
					type=text size=12 name=tglmin value='<?=$tglmin?>'> &nbsp; Max : <input
					type=text size=12 name=tanggal value='<?=$tgl?>'> &nbsp; <input
					type=submit name='backup' value='Refresh'> <input type=submit
					name='backup' value='Transfer'>
				<p>
				<?=$strtampil?>
				</p>
			</form>
		</td>
	</tr>
</table>
				<?
				require("footer.inc.php");
				?>
